package com.zhiwang.planet.widget

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.zhiwang.planet.R
import com.zhiwang.planet.mvvm.model.entity.main.VideoClipTaskResult
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.advideo_dialog_layout.*
import kotlinx.android.synthetic.main.advideo_dialog_layout.view.*
import org.reactivestreams.Subscriber
import org.reactivestreams.Subscription
import java.util.concurrent.TimeUnit

class AdVideoDialog constructor(var dialog: AlertDialog,result:VideoClipTaskResult/*, var reward: String, var msg: String, var deadline: Int = 23*/) {


    companion object SOUL {

        fun create(activity: AppCompatActivity,result:VideoClipTaskResult /*reward: String, msg: String, deadline: Int, completed: Boolean*/): AdVideoDialog {

            val rootView = activity.layoutInflater.inflate(R.layout.advideo_dialog_layout, null)

            val dialog = AlertDialog.Builder(activity)
                    .setView(rootView)
                    .create()
            dialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            rootView.msg.text = "共可领取${result.max}次"
            rootView.reward.text = "+${result.reward}"
            rootView.count_down_time.text = "${result.num}次"
//            if (completed)
//                rootView.video_task_msg.text = "明日再来观看"
            rootView.close_btn.setOnClickListener { dialog.dismiss() }
            return AdVideoDialog(dialog,result)
        }
    }


    fun show() {
        dialog.show()

       /* Flowable.interval(1, TimeUnit.SECONDS)
                .map {
                    deadline--
                    deadline
                }
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Subscriber<Int> {

                    var subscription: Subscription? = null

                    override fun onComplete() {
                    }

                    override fun onSubscribe(s: Subscription?) {
                        subscription = s
                        subscription?.request(1)
                    }

                    override fun onNext(t: Int) {
                        if (t == 0)
                            dialog.dismiss()
                        else dialog.count_down_time.text = t.toString()
                        subscription?.request(1)
                    }

                    override fun onError(t: Throwable?) {

                    }

                })*/

    }

}